package cn.jwcat.controller;

import cn.jwcat.dealEntity.DealHealth;
import cn.jwcat.dealEntity.UserHealthDto;
import cn.jwcat.entity.UserHealth;
import cn.jwcat.service.IUserHealthService;
import cn.jwcat.utils.JwtTokenUtil;
import cn.jwcat.utils.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户健康信息表操作
 */
@RestController
@RequestMapping("/uhealth")
public class UserHealthController {
	@Resource
	JwtTokenUtil jwtTokenUtil;

	@Resource
	IUserHealthService userHealthService;

	@PostMapping
	@ApiOperation("患者提交健康信息")
	public Result uHealthPost(@RequestBody UserHealth userHealth, HttpServletRequest request) {
// TODO: 2023-5-5 重复打卡的逻辑
		Integer id = jwtTokenUtil.getIDFromRequest(request);
		userHealth.setUserId(id);
		boolean save = userHealthService.save(userHealth);
		if (save) return Result.success("打卡成功");
		return Result.error().message("打卡失败");
	}

	@GetMapping("/{page}/{size}")
	@ApiOperation("获取账号状态激活的患者的健康信息")
	public Result getHealthInfoByStatus(@PathVariable("page") int page, @PathVariable("size") int size) {
		PageHelper.startPage(page, size);
		List<UserHealthDto> healthDtos = userHealthService.getHealthInfoByStatus();
		PageInfo<UserHealthDto> info = new PageInfo<>(healthDtos);
		return Result.success(info);
	}

	@PostMapping("/{page}/{size}")
	@ApiOperation("健康信息查询")
	public Result postQuery(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody DealHealth dealHealth) {
		PageHelper.startPage(page, size);
		List<UserHealthDto> healthDtos = userHealthService.postQuery(dealHealth);
		PageInfo<UserHealthDto> info = new PageInfo<>(healthDtos);
		return Result.success(info);
	}
}
